#include "snake.h"
#include <cstdlib>

// 创建游戏地图
void createMap()
{
  clear();
  attron(COLOR_PAIR(1));
  // 绘制顶部和底部边框
  for (int i = 0; i < maxX; i++)
  {
    mvprintw(0, i, "#");
    mvprintw(maxY - 3, i, "#");
  }
  // 绘制左右边框
  for (int i = 0; i < maxY - 2; i++)
  {
    mvprintw(i, 0, "#");
    mvprintw(i, maxX - 1, "#");
  }
  attroff(COLOR_PAIR(1));
  refresh();
}

// 初始化蛇
void initsnake()
{
  int startX = maxX / 2;
  int startY = maxY / 2;
  snakeLength = 5;
  for (int i = 0; i < snakeLength; i++)
  {
    snake[i].x = startX - i;
    snake[i].y = startY;
  }
  attron(COLOR_PAIR(2));
  for (int i = 1; i < snakeLength; i++)
  {
    mvprintw(snake[i].y, snake[i].x, "*");
  }
  mvprintw(snake[0].y, snake[0].x, "@");
  attroff(COLOR_PAIR(2));
  refresh();
}

// 创建食物
void createfood()
{
  bool validPosition;
  do
  {
    validPosition = true;
    foodX = rand() % (maxX - 2) + 1;
    foodY = rand() % (maxY - 4) + 1;
    for (int i = 0; i < snakeLength; i++)
    {
      if (foodX == snake[i].x && foodY == snake[i].y)
      {
        validPosition = false;
        break;
      }
    }
  } while (!validPosition);
  attron(COLOR_PAIR(3));
  mvprintw(foodY, foodX, "O");
  attroff(COLOR_PAIR(3));
  refresh();
}